﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Maticsoft.Web.Admin.Shopping
{
    public partial class Category : PageBase
    {
        BLL.Categorys bll = new BLL.Categorys();

        protected void Page_Load(object sender, EventArgs e)
        {
            Check("Shopping", "read");
            LoadList();
        }

        /// <summary>
        /// 加载整个分类表
        /// </summary>
        private void LoadList()
        {
            litlist.Text = "";

            int MTcount = 0;//执行遍历的次数
            //开始迭代加载
            MakeTr("0", MTcount);

        }
        /// <summary>
        /// 装配每一行
        /// </summary>
        /// <param name="id">上级ID</param>
        /// <param name="count">迭代次数</param>
        private void MakeTr(string id, int count)
        {
            DataView dv = new DataView(bll.GetList("parentID=" + id + " and IsActive=1 order by CategoryId asc").Tables[0]);
            foreach (DataRowView drv in dv)
            {
                //形成表格
                if (drv["parentID"].ToString() == "0")
                {
                    litlist.Text += "<tr bgcolor='#FFFFFF'><td>" + drv["CategoryId"].ToString() + "</td><td>" + MakeFelgefu(count) + drv["CategoryName"].ToString() + "</td><td align='center'>" + Convert.ToString(drv["OrderNo"]) + "</td><td align='center'>" + Convert.ToDateTime(drv["CreateTime"]).ToString("yyyy-MM-dd") + "</td><td align='center'><a href='EditCategory.aspx?act=edit&id=" + drv["CategoryId"].ToString() + "'>修改</a></td></tr>";
                }
                else
                {
                    litlist.Text += "<tr bgcolor='#FFFFFF'><td>" + drv["CategoryId"].ToString() + "</td><td>" + MakeFelgefu(count) + drv["CategoryName"].ToString() + "</td><td align='center'>" + Convert.ToString(drv["OrderNo"]) + "</td><td align='center'>" + Convert.ToDateTime(drv["CreateTime"]).ToString("yyyy-MM-dd") + "</td><td align='center'><a href='EditCategory.aspx?act=edit&id=" + drv["CategoryId"].ToString() + "'>修改</a></td></tr>";
                }

                //再次遍历
                MakeTr(drv["CategoryId"].ToString(), count + 1);

            }

        }
        /// <summary>
        /// Make分隔符
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        private string MakeFelgefu(int count)
        {
            string Returnwords = string.Empty;
            if (count == 0)
            {
                Returnwords = "";
            }

            else
            {
                Returnwords = ("┠").PadLeft(count, '　');
            }
            return Returnwords;
        }
    }
}